package com.jdyfy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdyfy.dto.ProjectAnnounceDTO;
import com.jdyfy.entity.AnnouncementDepartment;
import com.jdyfy.entity.Project;
import com.jdyfy.mapper.AnnouncementDepartmentMapper;
import com.jdyfy.service.AnnouncementDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AnnouncementDepartmentImpl extends ServiceImpl<AnnouncementDepartmentMapper, AnnouncementDepartment> implements AnnouncementDepartmentService {

    @Autowired
    private AnnouncementDepartmentMapper adMapper;

    @Autowired
    private ProjectServiceImpl projectService;

    // 根据公告名称集合获取报名企业集合
    public List<AnnouncementDepartment> getEnlistsDepartment(List<String> announcements) {
        return adMapper.getEnlistsDepartment(announcements);
    }

    //根据公告名称获取报名企业
    public AnnouncementDepartment getEnlistsDepartments(String announcementName) {
        return adMapper.getEnlistsDepartments(announcementName);
    }

    //根据企业名称获取报名公告
    @Override
    public List<ProjectAnnounceDTO> getEnlistsAnnouncementsBydepartment(String departmentName,String projectName,String projectNumber) {
        return adMapper.getEnlistsAnnouncementsBydepartment(departmentName,projectName,projectNumber);
    }

    //获取报名企业数量
    public void getEnrollCount(String announcementName) {
        int enrollCount = adMapper.getEnrollCount(announcementName);
        if(enrollCount == 3){ // 报名企业满三个了，将项目状态修改置为3，公告状态置为2
            // 查找 '{' 和 '+' 的位置
            int startIndex = announcementName.indexOf("{") + 1;  // '{' 后面的位置
            int endIndex = announcementName.indexOf("+");  // '+' 的位置
            // 截取中间部分
            String projectNumber = announcementName.substring(startIndex, endIndex);
            Project project = new Project();
            project.setProjectNumber(projectNumber);
            project.setStatus(3);
            project.setAnnoStatus(2);
            projectService.saveOrUpdate(project);
        }
    }

    public int getAdIsHave(String announcementName, String departmentName) {
        return adMapper.getAdIsHave(announcementName, departmentName);
    }
}
